$().ready(function() {
	loadProject();
	loadsupplier();
	validateRule();
	laydate({
		elem : '#signTime'
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/pur/group/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			groupid : {
				required : true
			},
			projectId : {
				required : true
			},
			purType :{
				required : true
			},
			supplierId :{
				required : true
			},
			allPurprice :{
				required : true
			},
			taxRate :{
				required : true
			},
			signTime:{
				required : true
			}
		},
		messages : {
			groupid : {
				required : icon + "请输入采购合同编号"
			},
			projectId : {
				required : icon + "请选择所属项目"
			},
			purType :{
				required : icon + "请选择采购类型"
			},
			supplierId :{
				required : icon + "请选择供应商"
			},
			allPurprice :{
				required : icon + "请输入采购总价"
			},
			taxRate :{
				required : icon + "请输入税率"
			},
			signTime :{
				required : icon + "请选择采购时间"
			}
		}
	})
}


//加载项目
function loadProject(){
	var html = "";
	$.ajax({
		url : '/itm/project/getNotEndProjectlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].itmNumber + '</option>'
			}
			$(".chosen-select-projectId").append(html);
			$(".chosen-select-projectId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-projectId').on('change', function(e, params) {
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}

function loadsupplier() {
	var html = "";
	$.ajax({
		url : '/pro/supplier/supplierlist',
		success : function(data) {
			//加载数据
			for (var i = 0; i < data.length; i++) {
				html += '<option value="' + data[i].id + '">' + data[i].name + '</option>'
			}
			$(".chosen-select-supplierId").append(html);
			$(".chosen-select-supplierId").chosen({
				maxHeight : 200
			});
			//点击事件
			$('.chosen-select-supplierId').on('change', function(e, params) {
				console.log(params.selected);
				var opt = {
					query : {
						type : params.selected,
					}
				}
				$('#exampleTable').bootstrapTable('refresh', opt);
			});
		}
	});
}